package com.xzp.blog.service.impl;

import com.xzp.blog.consts.Message;
import com.xzp.blog.consts.SessionConstants;
import com.xzp.blog.entity.Admin;
import com.xzp.blog.entity.dto.form.AdminUserPwdModifyForm;
import com.xzp.blog.entity.dto.form.UserLoginForm;
import com.xzp.blog.entity.dto.request.TableKeyModel;
import com.xzp.blog.mapper.AdminMapper;
import com.xzp.blog.service.IAdminService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

import static com.xzp.blog.consts.SessionConstants.SESSION_ADMIN_CURRENT_USER;

@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin adminLoginCheck(UserLoginForm userLoginForm) {
        Admin admin = new Admin();
        admin.setUsername(userLoginForm.getUsername());
        admin.setPassword(DigestUtils.md5Hex(userLoginForm.getPassword()));
        List<Admin> adminList = adminMapper.select(admin);
        if (adminList != null && adminList.size() > 0)
            return adminList.get(0);

        return null;
    }

    /**
     * 将当前管理员用户加入到Session中
     * @param request
     * @param admin
     */
    @Override
    public void joinSession(HttpServletRequest request, Admin admin) {
        HttpSession session = request.getSession(true);
        session.setAttribute(SESSION_ADMIN_CURRENT_USER,admin);
    }

    /**
     * 管理员注销
     * @param request
     */
    @Override
    public void destroySession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(SESSION_ADMIN_CURRENT_USER);
    }

    /**
     * 修改密码
     */
    @Override
    public String modifyUserPwd(AdminUserPwdModifyForm form, HttpServletRequest request) {
        String oriPwd = form.getOriPwd();
        String newPwd = form.getNewPwd();
        String confirmPwd = form.getConfirmPwd();
        if (!newPwd.equals(confirmPwd)){
            return Message.NOT_EQUAL;
        }
        //如果密码一致,则验证原密码是否正确
        //从session中获取当前管理员
        Admin admin = (Admin) request.getSession().getAttribute(SessionConstants.SESSION_ADMIN_CURRENT_USER);

        if (StringUtils.equals(DigestUtils.md5Hex(oriPwd),admin.getPassword())){
            //如果密码正确
            admin.setPassword(DigestUtils.md5Hex(newPwd));
            adminMapper.updateByPrimaryKeySelective(admin);
            return Message.MODIFY_SUCCESS;
        }else {
            return Message.ORI_PWD_ERROR;
        }

    }

    @Override
    public List<Admin> getAdminUserJson() {
        return adminMapper.selectAll();
    }


    /**
     * 删除用户
     */
    @Override
    public void deleteAdminUser(TableKeyModel model) {
        for (Integer id : model.getIds()) {
            adminMapper.deleteByPrimaryKey(id);
        }
    }
}
